<?php
class paging
{
	var $total_record 	= 150;				//Tong so record
	var $page_limit 	= 15;				//So record tren mot trang
	var $num_url		= 5;				//So Link hien thi
	var $url 			= '';				//Link lien ket
	var $first_page 	= 'First';
	var $last_page 		= 'Last';
	var $span			= '&nbsp;';
	var $curr_start		= '<b>';
	var $curr_end		= '</b>';
	var $anchor_class	= '';				//Class danh cho phan trang
	function FirstPage()
	{
		return '<a href="'.$this->url.'1" class="'.$this->anchor_class.'">'.$this->first_page.'</a>';
	}
	function LastPage()
	{
		$last_page = ceil($this->total_record/$this->page_limit);
		return '<a href="'.$this->url.$last_page.'" class="'.$this->anchor_class.'">'.$this->last_page.'</a>';
	}
	function TotalPage()
	{
		return ceil($this->total_record/$this->page_limit);
	}
	function CreateLink($page)
	{
		$link = '';
		if($this->TotalPage() <= 1)
			return $link;
		if($this->num_url >= $this->TotalPage())
		{
			for($i=1;$i<=$this->TotalPage();$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<a href="'.$this->url.$i.'" class="'.$this->anchor_class.'">'.$i.'</a>'.$this->span;
			}
			return $link;
		}
		if(($page-(int)($this->num_url/2)) <= 1)
		{
			if(($page-ceil($this->num_url/2)) < 1)
				$i=1;
			else
				$i=($page-ceil($this->num_url/2));
			for($i;$i<=$this->num_url;$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<a href="'.$this->url.$i.'" class="'.$this->anchor_class.'">'.$i.'</a>'.$this->span;
			}
			$link .= $this->LastPage();
		}
		elseif(($page+ceil($this->num_url/2)) > $this->TotalPage())
		{
			$link .= $this->FirstPage().$this->span;
			for($i=(($this->TotalPage()-$this->num_url)+1);$i<=$this->TotalPage();$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<a href="'.$this->url.$i.'" class="'.$this->anchor_class.'">'.$i.'</a>'.$this->span;
			}
		}
		else
		{
			$link .= $this->FirstPage().$this->span;
			for($i=($page-(int)($this->num_url/2));$i<($page+ceil($this->num_url/2));$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<a href="'.$this->url.$i.'" class="'.$this->anchor_class.'">'.$i.'</a>'.$this->span;
			}
			$link .= $this->LastPage();
		}
		return $link;
	}
	function CreateAjaxLink($page)
	{
		$link = '';
		if($this->TotalPage() <= 1)
			return $link;
		if($this->num_url >= $this->TotalPage())
		{
			for($i=1;$i<=$this->TotalPage();$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<span class="'.$this->anchor_class.'">'.$i.'</span>'.$this->span;
			}
			return $link;
		}
		if(($page-(int)($this->num_url/2)) <= 1)
		{
			if(($page-ceil($this->num_url/2)) < 1)
				$i=1;
			else
				$i=($page-ceil($this->num_url/2));
			for($i;$i<=$this->num_url;$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<span class="'.$this->anchor_class.'">'.$i.'</span>'.$this->span;
			}
			$link .= '<span class="'.$this->anchor_class.'">'.$this->last_page.'</span>';
		}
		elseif(($page+ceil($this->num_url/2)) > $this->TotalPage())
		{
			$link .= '<span class="'.$this->anchor_class.'">'.$this->first_page.'</span>'.$this->span;
			for($i=(($this->TotalPage()-$this->num_url)+1);$i<=$this->TotalPage();$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<span class="'.$this->anchor_class.'">'.$i.'</span>'.$this->span;
			}
		}
		else
		{
			$link .= '<span class="'.$this->anchor_class.'">'.$this->first_page.'</span>'.$this->span;
			for($i=($page-(int)($this->num_url/2));$i<($page+ceil($this->num_url/2));$i++)
			{
				if($page == $i)
					$link .= $this->curr_start.$i.$this->curr_end.$this->span;
				else
					$link .= '<span class="'.$this->anchor_class.'">'.$i.'</span>'.$this->span;
			}
			$link .= '<span class="'.$this->anchor_class.'">'.$this->last_page.'</span>';
		}
		return $link;
	}
	function SQLlimit($page, $pagelimit)
	{
		$page = ($page-1)*$pagelimit;
		return $page.','.$pagelimit;
	}
}